package ch.rheintalconnection.sysventory.businessServices;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import ch.rheintalconnection.sysventory.domain.Client;
import ch.rheintalconnection.sysventory.domain.Computer;
import ch.rheintalconnection.sysventory.domain.NIC;
import ch.rheintalconnection.sysventory.domain.OperatingSystem;
import ch.rheintalconnection.sysventory.domain.Printer;
import ch.rheintalconnection.sysventory.domain.Processor;
import ch.rheintalconnection.sysventory.domain.Program;
import ch.rheintalconnection.sysventory.dto.ComputerDto;
import ch.rheintalconnection.sysventory.dto.LogicalDiskDto;
import ch.rheintalconnection.sysventory.dto.NicDto;
import ch.rheintalconnection.sysventory.dto.OperatingSystemDto;
import ch.rheintalconnection.sysventory.dto.PrinterConfigurationDto;
import ch.rheintalconnection.sysventory.dto.PrinterDto;
import ch.rheintalconnection.sysventory.dto.ProcessorDto;
import ch.rheintalconnection.sysventory.dto.ProgramDto;

public class DtoToDomainConverter {

	public Client toClient(NicDto nicDto) {
		
		String macAddress = nicDto.getMacAddress();
		return new Client(macAddress);
	}
	
	public Computer toComputer(ComputerDto computerDto,
			LogicalDiskDto logicalDiskDto) {
		String computerName = computerDto.getComputerName();
		String sRam = computerDto.getRam();
		long ram = Long.parseLong(sRam);
		String computerType = computerDto.getComputerType(); //TODO: Anhand int zuweisen.
		
		String sHdCapacity = logicalDiskDto.getHdcapacity();
		long hdCapacity = Long.parseLong(sHdCapacity);
		
		String sFreespace = logicalDiskDto.getFreespace();
		long freespace = Long.parseLong(sFreespace);
		long hdOccupied = hdCapacity - freespace;
		
		Computer computer = new Computer(computerName, ram, computerType, hdCapacity, hdOccupied);
		return computer;
	}
	
	public NIC toNic(NicDto nicDto) throws UnknownHostException {
		String macAddress = nicDto.getMacAddress().trim();
		
		String sIpAddress = nicDto.getIpAddress().trim();
		sIpAddress = sIpAddress.split(" ")[0];
		InetAddress ipAddress = InetAddress.getByName(sIpAddress);
		
		String description = nicDto.getDescription();
		
		String sGateway = nicDto.getGateway().trim();
		InetAddress gateway = InetAddress.getByName(sGateway);
		
		String sDhcp = nicDto.getDhcp().trim();
		InetAddress dhcp = InetAddress.getByName(sDhcp);
		
		String sSubNet = nicDto.getSubNet().trim();
		sSubNet = sSubNet.split(" ")[0];
		InetAddress subNet = InetAddress.getByName(sSubNet);
		NIC nic = new NIC(ipAddress, description, macAddress, gateway, dhcp, subNet);
		return nic;
	}
	
	public OperatingSystem toOperatingSystem(
			OperatingSystemDto operatingSystemDto) {
		String osDescription = operatingSystemDto.getDescription();
		String osVersion = operatingSystemDto.getVersion();
		String osTechnology = operatingSystemDto.getTechnology();
		OperatingSystem os = new OperatingSystem(osDescription, osVersion, osTechnology);
		return os;
	}
	
	public Processor toProcessor(ProcessorDto processorDto) {
		String processorDescription = processorDto.getDescription();
		String processorFamily = processorDto.getFamily();
		String sProcessorCores = processorDto.getCores();
		int processorCores = Integer.parseInt(sProcessorCores);
		String processorClockrate = processorDto.getClockrate();
		Processor processor = new Processor(processorDescription, processorFamily, processorCores, processorClockrate);
		return processor;
	}

	public List<Printer> toPrinters(List<PrinterDto> printerDtos,
			List<PrinterConfigurationDto> printerConfigurationDtos) {
		
		List<Printer> printers = new ArrayList<Printer>();
		
		for (int i = 0; i < printerDtos.size(); i++) {
			
			PrinterDto printerDto = printerDtos.get(i);
			PrinterConfigurationDto printerConfigurationDto = printerConfigurationDtos.get(i);
			
			String description = printerDto.getDescription();
			String driver = printerDto.getDriver();
			String version = printerConfigurationDto.getVersion();
			
			printers.add(new Printer(description, driver, version));
		}
		return printers;
	}

	public List<Program> toPrograms(List<ProgramDto> programDtos) {
		
		List<Program> programs = new ArrayList<Program>();
		
		for (ProgramDto programDto : programDtos) {
			
			String description = programDto.getDescription();
			String version = programDto.getVersion();
			
			programs.add(new Program(description, version));
		}
		return programs;
	}

}
